package com.usian.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.feign.ArticleFeign;
import com.usian.mapper.ApUserFanMapper;
import com.usian.mapper.ApUserFollowMapper;
import com.usian.mapper.ApUserMapper;
import com.usian.model.article.pojos.ApAuthor;
import com.usian.model.behavior.dto.FollowBehaviorDto;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.dtos.UserRelationDto;
import com.usian.model.user.pojos.ApUser;
import com.usian.model.user.pojos.ApUserFan;
import com.usian.model.user.pojos.ApUserFollow;
import com.usian.service.ApUserFollowService;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class ApUserFollowServiceImpl extends ServiceImpl<ApUserFollowMapper, ApUserFollow> implements ApUserFollowService {

    @Autowired
    private ApUserFanMapper apUserFanMapper;
    @Autowired
    private ArticleFeign articleFeign;
    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private KafkaTemplate kafkaTemplate;


    @Override
    public ResponseResult follow(UserRelationDto dto) {
        //1：校验参数
        if(dto==null || dto.getAuthorId()==null || dto.getArticleId()==null || dto.getOperation()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }

        //2: 查询作者昵称  article ap_author
        ApAuthor apAuthor = articleFeign.findById(dto.getAuthorId());
        if(apAuthor==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"查询作者对象不存在");
        }
        ApUser apUser = AppThreadLocalUtils.getUser();

        //3: 关注  插入
        if(dto.getOperation()==0){
            //关注过的用户我不需要重复关注
            if(apUser!=null){
                ApUserFollow apUserFollow = this.getOne(Wrappers.<ApUserFollow>lambdaQuery()
                        .eq(ApUserFollow::getUserId, apUser.getId()).eq(ApUserFollow::getFollowId, apAuthor.getUserId()));
                if(apUserFollow==null){
                    //插入数据
                    //保存app用户关注信息
                    apUserFollow = new ApUserFollow();
                    apUserFollow.setUserId(apUser.getId());
                    apUserFollow.setFollowId(apAuthor.getUserId());
                    apUserFollow.setFollowName(apAuthor.getName());
                    apUserFollow.setCreatedTime(new Date());
                    apUserFollow.setIsNotice(true);
                    apUserFollow.setLevel((short)1);
                    this.save(apUserFollow);

                    //插入 fan表
                    ApUserFan apUserFan = apUserFanMapper.selectOne(Wrappers.<ApUserFan>lambdaQuery()
                            .eq(ApUserFan::getUserId, apAuthor.getUserId()).eq(ApUserFan::getFansId, apUser.getId()));
                    if(apUserFan==null){
                        apUserFan = new ApUserFan();
                        apUserFan.setUserId(apAuthor.getUserId());
                        apUserFan.setFansId(apUser.getId().longValue());
                        ApUser apUser1 = apUserMapper.selectById(apUser.getId());
                        apUserFan.setFansName(apUser1.getName());
                        apUserFan.setLevel((short)0);
                        apUserFan.setIsDisplay(true);
                        apUserFan.setIsShieldLetter(false);
                        apUserFan.setIsShieldComment(false);
                        apUserFan.setCreatedTime(new Date());
                        apUserFanMapper.insert(apUserFan);
                    }
                }
            }

            //当关注用户后需要发送kafka消息 完成记录用户关注行为
            FollowBehaviorDto dto1 =new FollowBehaviorDto();
            dto1.setUserId(apUser.getId());
            dto1.setFollowId(apAuthor.getUserId());
            dto1.setArticleId(dto.getArticleId());
            kafkaTemplate.send("followTopic", JSON.toJSONString(dto1));

        }else if(dto.getOperation()==1){
            //4:  取消关注  删除
            ApUserFollow apUserFollow = this.getOne(Wrappers.<ApUserFollow>lambdaQuery()
                    .eq(ApUserFollow::getUserId, apUser.getId()).eq(ApUserFollow::getFollowId, apAuthor.getUserId()));
            if(apUserFollow==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"未关注");
            }else{
                this.remove(Wrappers.<ApUserFollow>lambdaQuery()
                        .eq(ApUserFollow::getUserId, apUser.getId()).eq(ApUserFollow::getFollowId, apAuthor.getUserId()));
            }

            ApUserFan apUserFan = apUserFanMapper.selectOne(Wrappers.<ApUserFan>lambdaQuery()
                    .eq(ApUserFan::getUserId, apAuthor.getUserId()).eq(ApUserFan::getFansId, apUser.getId()));
            if(apUserFan==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"未关注");
            }else{
                apUserFanMapper.delete(Wrappers.<ApUserFan>lambdaQuery()
                        .eq(ApUserFan::getUserId, apAuthor.getUserId()).eq(ApUserFan::getFansId, apUser.getId()));
            }

        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
